package com.chrilwe.pigpig.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.chrilwe.pigpig.auth.common.PhoneConfigProperty;

@Configuration
public class PhoneConfig {
	
	@Autowired
	protected PhoneConfigProperty phoneConfigProperty;
	
	@Bean
	public PhoneConfigProperty phoneConfigProperty() {
		PhoneConfigProperty property = new PhoneConfigProperty();
		property.setAccessKeyId("LTAI4G4pqMp3t5KUWJBcEn6z");
		property.setAccessKeySecret("bZZAseedHiSQL4yC38PFDsiWr2iIqL");
		property.setDefaultConnectTimeout("10000");
		property.setDefaultReadTimeout("10000");
		property.setDomain("dysmsapi.aliyuncs.com");
		property.setProduct("Dysmsapi");
		property.setRegion("cn-hangzhou");
		property.setSignName("方糗事百科");
		property.setTemplateCode("SMS_201721147");
		property.setPhoneCodeExpire(5);//手机验证码有效期（分钟）
		return property;
	}

	@Bean("acsClient")
	public IAcsClient acsClient() {
		// 设置超时时间-可自行调整
		System.setProperty("sun.net.client.defaultConnectTimeout", phoneConfigProperty.getDefaultConnectTimeout());
		System.setProperty("sun.net.client.defaultReadTimeout", phoneConfigProperty.getDefaultReadTimeout());
		// 初始化ascClient需要的几个参数
		final String product = phoneConfigProperty.getProduct();// 短信API产品名称（短信产品名固定，无需修改）
		final String domain = phoneConfigProperty.getDomain();// 短信API产品域名（接口地址固定，无需修改）
		// 替换成你的AK
		final String accessKeyId = phoneConfigProperty.getAccessKeyId();// 你的accessKeyId,参考本文档步骤2
		final String accessKeySecret = phoneConfigProperty.getAccessKeySecret();// 你的accessKeySecret，参考本文档步骤2
		// 初始化ascClient,暂时不支持多region（请勿修改）
		IClientProfile profile = DefaultProfile.getProfile(phoneConfigProperty.getRegion(), accessKeyId, accessKeySecret);
		try {
			DefaultProfile.addEndpoint(phoneConfigProperty.getRegion(), phoneConfigProperty.getRegion(), product, domain);
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		IAcsClient acsClient = new DefaultAcsClient(profile);
		return acsClient;
	}

}
